<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

/**
 * PENGUIN FrameWork
 * Các hàm về field support view
 * 
 * @author      hungtd <tdhungit@gmail.com> 0972014011
 * @copyright   Tran Dinh Hung 2011
 * @package     PenguinFW
 * @subpackage  Helper
 * @version     1.0.0
 */
/**
 * In giá trị sau khi check field
 * 
 * @param   string $value
 * @param   int $type 
 * @return  string value
 */
if (!function_exists('pg_field_value'))
{
    function pg_field_value($value, $type)
    {
        switch ($type)
        {
            case ConstFieldType::date:
                $CI =& get_instance();
                // load hepler date
                $CI->load->helper('date');
                // get date
                return mdate(config_item('pg-date-format'), strtotime($value));
                break;
            case ConstFieldType::datetime:
                $CI =& get_instance();
                // load hepler date
                $CI->load->helper('date');
                // get date
                return mdate(config_item('pg-datetime-format'), strtotime($value));
                break;
            case ConstFieldType::text:
            case ConstFieldType::num:
            default:
                return $value;
                break;
        }
    }
}

/**
 * Get username from user id
 * 
 * @param int $user_id user ID
 * @return string username
 */
if (!function_exists('user_name'))
{    
    function user_name($user_id)
    {
        $CI =& get_instance();
        
        $CI->db->select('username');
        $user = $CI->db->get_where('users', array('id' => $user_id));
        
        if ($user->num_rows() == 0) return false;
        
        return $user->row()->username;
    }
}

/**
 * Config and get link pagination
 * 
 * @param string $base_uri
 * @param int $total_rows
 * @param int $per_page
 * @param string $open_tag
 * @param string $close_tag
 * @return string
 */
if (!function_exists('pagination_config'))
{
    function pagination_config($base_uri, $total_rows, $per_page = 20, $segment = 5, $open_tag = '<div class="pagination">', $close_tag = '</div>')
    {
        $CI =& get_instance();
        
        $CI->load->library('pagination');

        $config = array(
            'base_url' => base_url($base_uri),
            'total_rows' => $total_rows,
            'per_page' => $per_page,
            'full_tag_open' => $open_tag,
            'full_tag_close' => $close_tag,
            'uri_segment' => $segment,
        );

        $CI->pagination->initialize($config);

        return $CI->pagination->create_links();
    }
}

/**
 * Get link action in list view
 * 
 * @param string|array $links
 * @param string $class class
 * @param string $prefix char bettween 2 action
 * @return string 
 */
if (!function_exists('link_action'))
{
    function link_action($links, $id = '', $class = 'action', $prefix = '')
    {
        // check multi link
        if (is_array($links) && !empty ($links))
        {
            $result_link = '';
            foreach ($links as $key => $link)
            {
                $result_link .= '<a href="' . base_url($link) . '/' . $id . '" class="' . $class . '">'. lang($key) .'</a>';
            }

            return $result_link;
        }

        if ($links)
        {
            return '<a href="' . base_url($links) . '/' . $id . '" class="' . $class . '">'. lang('Edit') .'</a>';
        }

        return '';
    }    
}

/**
 * Debug by PG
 * 
 * @param type $var 
 */
if (!function_exists('pg_debug'))
{
    function pg_debug($var, $is_die = TRUE)
    {
        echo '<pre>';
        print_r($var);
        echo '</pre>';
        
        if ($is_die == TRUE)
        {
            die();
        }
    }
}

/**
 * Chuyển dạng date sang dạng chuẩn SQL
 * 
 * @param string $char_standar Dấu phân cách dạng chuẩn
 * @param string $char Dấu phân cách dạng hiện tại
 * @return string yyyy/mm/dd 
 */
if (!function_exists('standar_date'))
{
    function standar_date($date, $char_standar = '-', $char = '/')
    {
        // convert date dd/mm/yyyy to array
        $date_array = explode($char, $date);

        // return date yyyy/mm/dd
        return $date_array[2] . $char_standar . $date_array[1] . $char_standar . $date_array[0];
    }
}

?>